package com.ly.mpayment.rest.system.yidong;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.BigUtil;
import com.ly.mpayment.core.util.ChaoJiYing;
import com.ly.mpayment.core.util.CookieRedis;
import com.ly.mpayment.core.util.DateUtils;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.persist.entity.UpOrder;
import com.ly.mpayment.persist.entity.yidong.GameOrder;
import com.ly.mpayment.persist.entity.yidong.MobileOrders;
import com.ly.mpayment.persist.service.downrechargeflow.IDownRechargeFlowService;
import com.ly.mpayment.persist.service.downshopinfo.IDownShopInfoService;
import com.ly.mpayment.persist.service.uporder.IUporderService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.persist.service.yidong.IGameOrderService;
import com.ly.mpayment.persist.service.yidong.IMobileOrdersService;
import com.ly.mpayment.rest.AbsRest;
//import com.ly.mpayment.rest.up.game.GameOrderRest;
//import com.ly.mpayment.rest.up.telecom.ChinaTelecom;
//import com.ly.mpayment.rest.up.yidong.SmsUtil;
//import com.ly.mpayment.rest.up.yidong.YiDongBus;
//import com.ly.mpayment.rest.up.yidong.YidongRest;
//import com.ly.mpayment.rest.up.yidong.wap.WapYiDongUtil;

import cn.jpush.api.utils.StringUtils;
import net.sf.json.JSONObject;

@Component
@Path("system/mobileorders")
public class MobileOrdersRest extends AbsRest {
	static Logger logger = Logger.getLogger(MobileOrdersRest.class);
	@Autowired
	IUserService userService;

	@Autowired
	IMobileOrdersService orderService;

//
//	@Autowired
//	YidongRest yidongRest;

	@Autowired
	ThreadPoolTaskExecutor taskExecutor;

	@Autowired
	IDownShopInfoService shopService;

	@Autowired
	IDownRechargeFlowService flowService;

//	@Autowired
//	SmsUtil smsRest;

	@Autowired
	IUporderService upOrderService;
//
//	@Autowired
//	GameOrderRest gameOrderRest;

	@Autowired
	IGameOrderService gameOrderService;

//	@Autowired
//	YiDongBus yidongBus;
//	@Autowired
//	WapYiDongUtil wapYidong;
//
//	@Autowired
//	ChinaTelecom telecom;

	@POST
	@Path("pageOrders")
	public String pageOrders(String data, @Context HttpServletRequest request){


		String result = this.validationParameter(data,request, userService, "start","pageSize","startTime","endTime",
				"province","city","serialNo","phoneNo","chargeMoney","orderStatus","noticeStatus","orderId","matching","mobileOrdersId");

		if(result == null){
			Map<String, Object> parameters = MapUtil.getMap();
			JSONObject json = JsonUtils.converToJson(data);
			if(!StringUtils.isEmpty(json.getString("startTime"))){
				parameters.put(":startTime", json.getString("startTime"));
			}
			if(!StringUtils.isEmpty(json.getString("endTime"))){
				parameters.put(":endTime", json.getString("endTime"));
			}
			if(!StringUtils.isEmpty(json.getString("province"))){
				parameters.put(":province", json.getString("province"));
			}
			if(!StringUtils.isEmpty(json.getString("city"))){
				parameters.put(":city", json.getString("city"));
			}
			if(!StringUtils.isEmpty(json.getString("serialNo"))){
				parameters.put(":serialNo", json.getString("serialNo"));
			}
			if(!StringUtils.isEmpty(json.getString("phoneNo"))){
				parameters.put(":phoneNo", json.getString("phoneNo"));
			}
			if(!StringUtils.isEmpty(json.getString("orderId"))){
				parameters.put(":orderId", json.getString("orderId"));
			}

			if(!StringUtils.isEmpty(json.getString("chargeMoney"))){
				parameters.put(":chargeMoney", BigUtil.getBigDecimal(json.getString("chargeMoney")));
			}
			if(!StringUtils.isEmpty(json.getString("orderStatus"))){
				parameters.put(":orderStatus", Integer.valueOf(json.getString("orderStatus")));
			}
			if(!StringUtils.isEmpty(json.getString("noticeStatus"))){
				parameters.put(":noticeStatus", Integer.valueOf(json.getString("noticeStatus")));
			}
			if(!StringUtils.isEmpty(json.getString("matching"))){
				parameters.put(":matching", Integer.valueOf(json.getString("matching")));
			}
			if(!StringUtils.isEmpty(json.getString("mobileOrdersId"))){
				parameters.put(":mobileOrdersId", (json.getString("mobileOrdersId")));
			}



			return ResultMessage.getSucceed( JsonUtils.excludes(this.orderService.pageOrders(parameters, json.getInt("start"), json.getInt("pageSize")),
					new String[]{"loginInfos","mobileOrderses"}));
		}
		return result;
	}

//	@SuppressWarnings("static-access")
//	@POST
//	@Path("searchSlowOrder")
//	public String searchSlowOrder(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request, userService, "mobileOrdersId");
//		if(result == null){
//			JSONObject json = new JSONObject().fromObject(data);
//			MobileOrders orders = this.orderService.findById(json.getString("mobileOrdersId"));
//			if(orders != null){
//				if(orders.getOperator() == 1){
//					String query = wapYidong.queryWapOrder(orders.getPayUrl(), orders.getOrderId());
//					if(query.equals("0")){
//						if(orders.getOrderStatus() == 1){
//							UpOrder order = upOrderService.findById(orders.getUpOrderId());
//							if(order != null && order.getStatus() == 1){
//								orderService.successMobileOrder(orders.getMobileOrdersId(), order, "充值成功", "4", "充值成功");
//							}
//						}
//					}
//					logger.info("searchSlowOrder:"+query);
//					return ResultMessage.getSucceed(null);
//				}else if(orders.getOperator() == 2){
//					return ResultMessage.getSucceed( telecom.payResult(orders.getOrderId()) ? "成功" : "成功");
//				}
//			}
//		}
//		return ResultMessage.getFailt("");
//	}

	@SuppressWarnings("static-access")
	@POST
	@Path("successSlowOrder")
	public String successSlowOrder(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request, userService, "mobileOrdersId");
//		if(result == null){
//			JSONObject json = new JSONObject().fromObject(data);
//			MobileOrders orders = this.orderService.findById(json.getString("mobileOrdersId"));
//			if(orders != null){
//				if(checkTime(DateUtils.getNowDate(), orders.getSystemTime()) && (orders.getOrderStatus() == 1 || orders.getOrderStatus() == 0) ){
//
//					UpOrder order = this.upOrderService.findById(orders.getUpOrderId());
//					if(order != null){
//						if(orders.getOperator() == 1){
//							logger.info("successSlowOrder:"+orders.getPhoneNo());
//							orders = this.orderService.findByFetchNumberPool(json.getString("mobileOrdersId"));
//							orderService.successMobileOrder( orders.getMobileOrdersId(), order, "手动充值成功", "4","充值成功页面");
//							return ResultMessage.getSucceed(null);
//						}else if(orders.getOperator() == 2){
//							orderService.successMobileOrder(orders.getMobileOrdersId(), order, "手动充值成功", "4","手工");
//							return ResultMessage.getSucceed(null);
//						}
//					}

//					orders.setOrderStatus(4);
//					orders.setMatching(1);
//					orders.setCallResult(1);
//					if(this.orderService.updateMobileOrders(orders)){
//						UpOrder order = this.upOrderService.findById(orders.getUpOrderId());
//						if(order != null){
//							//回调下游订单
//							flowService.orderHanlder(order, upOrderService, flowService, shopService, taskExecutor, null);
//							logger.error("successSlowOrder订单回调成功:"+orders.getPhoneNo());
//						}else{
//							logger.error("successSlowOrder订单回调失败:"+orders.getPhoneNo());
//						}
//						return ResultMessage.getSucceed(null);
//					}
//					return ResultMessage.getFailt("");
//				}else{
//					return ResultMessage.getFailt( CookieRedis.get("yidongordertime")+"分钟内的订单不能操作或订单状态不正确");
//				}
//			}
//			return ResultMessage.getFailt( "订单不存在");
//		}
		return ResultMessage.getFailt("");
	}

	/**
	 * 几分钟之内的订单不能操作
	 * @param date1
	 * @param date2
	 * @return
	 */
	private boolean checkTime(Date date1,Date date2){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date2);
		cal.add(Calendar.MINUTE,  Integer.valueOf(CookieRedis.get("yidongordertime")));
		return DateUtils.compareDate(date1,cal.getTime());
	}

	@SuppressWarnings("static-access")
	@POST
	@Path("failtSlowOrder")
	public  String failtSlowOrder(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request, userService, "mobileOrdersId");
//		if(result == null){
//			JSONObject json = new JSONObject().fromObject(data);
//			MobileOrders orders = this.orderService.findById(json.getString("mobileOrdersId"));
//			if(orders != null){
//				if(checkTime(DateUtils.getNowDate(), orders.getSystemTime())  && (orders.getOrderStatus() == 1 || orders.getOrderStatus() == 0)){
//					UpOrder order = this.upOrderService.findById(orders.getUpOrderId());
//					if(order != null){
//						if(orders.getOperator() == 1){
//							orders = this.orderService.findByFetchNumberPool(json.getString("mobileOrdersId"));
//							logger.info("failtSlowOrder:"+orders.getPhoneNo());
//							orderService.failtMobileOrder(  orders.getMobileOrdersId(), order, "手动充值失败", "5", orders.getMatching() == 1? true : false);
//							return ResultMessage.getSucceed(null);
//						}else if(orders.getOperator() == 2){
////							if(orders.getMatching() == 1){
//							orderService.failtHandlerMobileOrder(orders.getMobileOrdersId(), order, "手动充值失败", "5", orders.getMatching() == 1 ? true : false);
////							}else{
////								telecom.failtMobileOrderError(orders.getMobileOrdersId(), order, "手动充值失败", "5", orders.getMatching() == 1 ? true : false);
////							}
//							return ResultMessage.getSucceed(null);
//						}
//					}
//					return ResultMessage.getFailt("");
//				}else{
//					return ResultMessage.getFailt(  CookieRedis.get("yidongordertime")+"分钟内的订单不能操作或订单状态不正确");
//				}
//			}
//			return ResultMessage.getFailt( "订单不存在");
//		}
		return ResultMessage.getFailt("");
	}

//	@SuppressWarnings("static-access")
//	@POST
//	@Path("orderdetail")
//	public  String orderdetail(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request, userService, "mobileOrdersId");
//		if(result == null){
//			JSONObject json = new JSONObject().fromObject(data);
//			MobileOrders orders = this.orderService.findByFetchNumberPool(json.getString("mobileOrdersId"));
//			if(orders != null){
//				if(orders.getOperator() == 1){
////					com.alibaba.fastjson.JSONObject resultJson = yidongRest.queryOrder(20,0,orders.getNumberPool().getMobilePhone(), DateUtils.toLongDate(orders.getPayTime()),orders.getOrderId());
////					if(resultJson != null){
////						json.clear();
////						json.put("orderStatus", resultJson.getString("orderStatus"));
////						return ResultMessage.getSucceed(resultJson.toString());
////					}
//
//					String query = wapYidong.queryWapOrder(orders.getPayUrl(), orders.getOrderId());
//					if(query.equals("0")){
//						if(orders.getOrderStatus() == 1){
//							UpOrder order = upOrderService.findById(orders.getUpOrderId());
//							if(order != null && order.getStatus() == 1){
//								orderService.successMobileOrder(orders.getMobileOrdersId(), order, "充值成功", "4", "充值成功");
//							}
//						}
//						json.put("orderStatus", "4");
//						return ResultMessage.getSucceed(json.toString());
//					}
//					json.put("orderStatus",query);
//					return ResultMessage.getSucceed(json.toString());
//				}else if(orders.getOperator() == 2){
//					if(telecom.payResult(orders.getOrderId())){
//						return ResultMessage.getSucceed(null);
//					}else{
//						return ResultMessage.getFailt("");
//					}
//				}
//				return ResultMessage.getFailt( "查询失败");
//			}
//			return ResultMessage.getFailt( "订单不存在");
//		}
//		return ResultMessage.getFailt("");
//	}
//
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("updateOrder")
//	public  String updateOrder(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request, userService, "mobileOrdersId");
//		if(result == null){
//			JSONObject json = new JSONObject().fromObject(data);
//			MobileOrders orders = this.orderService.findByFetchNumberPool(json.getString("mobileOrdersId"));
//			if(orders != null){
//				if(orders.getOperator() == 1){
//					yidongRest.queryYiDongOrder(orders, orders.getNumberPool());
//					return ResultMessage.getSucceed(null);
//				}else{
//					telecom.payResult(orders.getOrderId());
//					return ResultMessage.getSucceed(null);
//				}
//			}
//			return ResultMessage.getFailt( "订单不存在");
//		}
//		return ResultMessage.getFailt("");
//	}
//
//
//
//	@SuppressWarnings("static-access")
//	@POST
//	@Path("pushgame")
//	public  String pushgame(String data,@Context HttpServletRequest request){
//		String result = this.validationParameter(data,request, userService, "mobileOrdersId");
//		if(result == null){
//			JSONObject json = new JSONObject().fromObject(data);
//			MobileOrders orders = this.orderService.findByFetchNumberPool(json.getString("mobileOrdersId"));
//			if(orders != null){
//				GameOrder gameOrder = gameOrderService.findByMobileOrderId(orders.getMobileOrdersId());
//				if(gameOrder != null){
//					gameOrderRest.callGame(gameOrder);
//					return ResultMessage.getSucceed(null);
//				}
//				return ResultMessage.getFailt("");
//
//			}
//			return ResultMessage.getFailt( "订单不存在");
//		}
//		return ResultMessage.getFailt("");
//	}

	@GET
	@Path("getScore")
	public  String getScore(@Context HttpServletRequest request){
		return ResultMessage.getSucceed(ChaoJiYing.GetScore());
	}
}
